package com.songlanyun.modules.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.MaterialConstant;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.material.dao.SystemMaterialDao;
import com.songlanyun.modules.material.entity.SystemMaterialEntity;
import com.songlanyun.modules.material.model.SystemMaterialCheckDTO;
import com.songlanyun.modules.material.model.SystemMaterialDTO;
import com.songlanyun.modules.material.service.SystemMaterialService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;


@Service("yxtSystemMaterialService")
public class SystemMaterialServiceImpl extends ServiceImpl<SystemMaterialDao, SystemMaterialEntity> implements SystemMaterialService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SystemMaterialEntity> page = this.page(
                new Query<SystemMaterialEntity>().getPage(params),
                new QueryWrapper<SystemMaterialEntity>().eq(
                        params.get("state") != null,"state",params.get("state")
                ).orderBy(true,false,
                        new String[]{"sort","create_time"}
                        ));
        return new PageUtils(page);
    }

    @Override
    public Boolean check(SystemMaterialCheckDTO dto) {
        ValidatorUtils.validateEntity(dto);
        SystemMaterialEntity entity = this.getById(dto.getId());
        if(entity != null){
            entity = new SystemMaterialEntity();
            entity.setId(dto.getId());
            entity.setState(MaterialConstant.StateEnum.getByCode(dto.getState()));
            entity.setPushTime(new Date());
            this.baseMapper.updateById(entity);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public SystemMaterialEntity addEntity(SystemMaterialDTO dto) {
        ValidatorUtils.validateEntity(dto);
        SystemMaterialEntity entity = new SystemMaterialEntity();
        //保存
        BeanUtil.copyPropertiesIgnoreNull(dto, entity);//克隆一份
        entity.setType(MaterialConstant.TypeEnum.getByCode(dto.getType()));

        this.save(entity);
        return entity;
    }


    @Override
    public SystemMaterialEntity updateEntity(SystemMaterialDTO dto) {
        ValidatorUtils.validateEntity(dto);
        //修改
        SystemMaterialEntity entity = null;
        if(dto.getId() != null){
            entity = this.getById(dto.getId());
            if(entity != null){
                BeanUtil.copyPropertiesIgnoreNull(dto, entity);//克隆一份
                entity.setType(MaterialConstant.TypeEnum.getByCode(dto.getType()));
                this.updateById(entity);
            }
        }
        return entity;
    }
}
